var enterpriseLogo={
    init:function(){
        var self=this;
        self.shadeDiv = $("#shade-div");
        self.setUp=$("ul.setUp")
        self.bindEvent();
        self.bindEvents();
        self.getMinHeight()
        self.jcropFunction();

    },
    bindEvent:function(){
        var self=this;
        $( ".popbox" ).draggable({ containment: "body", scroll: false,handle:'.popbox-hd' });

        //设置ajax请求时候出现loading
        $("#loading").ajaxStart(function(){
            $(this).fadeIn("fast");
        }).ajaxStop(function(){
                $(this).delay(1000).fadeOut("fast");
            });
        //侧边导航
        $(".nav-hd").click(function(){
            var $parent = $(this).parent();
            var subList = $parent.find(".sub-list");
            if(subList.is(":hidden")){
                $(this).addClass("arrow-down").removeClass("arrow-up");
                $parent.siblings().find(".sub-list").hide();
                $parent.siblings().find(".nav-hd").removeClass("arrow-down").addClass("arrow-up");
                subList.show();
            }else{
                $(this).removeClass("arrow-down").addClass("arrow-up");
                subList.hide();
            }
        });
        $(".sub-list li").click(function(){
            var $parent = $(this).closest(".parent-menu");
            $(this).addClass("cur").siblings().removeClass("cur");
            $parent.siblings().find("li").removeClass("cur");
        });
    },
    getMinHeight:function(){
        var that = this;
        var minHeight = $(window).height() - $("#header").outerHeight() - $("#footer").outerHeight();
        $("#main").css({'min-height':minHeight});
        $("#side").css({'height':$("#main").height()});
    },
    bindEvents:function(){
        var self=this;
        $(".switch a").click(function(){
            $(this).prev("input").removeAttr("disabled")
        })
        //        修改头像
        $("a.edit").click(function(){
            self.shadeDiv.show();
            $("#edit").css("height","auto").setPosition();
        })
        //        关闭/取消弹出层
        $(".closePopBtn,.cancelBtn").live("click",function(){
            $(".popbox").css("left","-10000px");
            $("#shade-div").hide();
        });

    },

    jcropFunction:function(){
        //记得放在jQuery(window).load(...)内调用，否则Jcrop无法正确初始化
        $("#bPic").Jcrop({
            onChange:showPreview,
            onSelect:showPreview,
            aspectRatio:1
        });
        //简单的事件处理程序，响应自onChange,onSelect事件，按照上面的Jcrop调用
        function showPreview(coords){
            if(parseInt(coords.w) > 0){
                //计算预览区域图片缩放的比例，通过计算显示区域的宽度(与高度)与剪裁的宽度(与高度)之比得到
                var rx = $("#preview_box").width() / coords.w;
                var ry = $("#preview_box").height() / coords.h;
                //通过比例值控制图片的样式与显示
                $("#crop_preview").css({
                    width:Math.round(rx * $("#bPic").width()) + "px",	//预览图片宽度为计算比例值与原图片宽度的乘积
                    height:Math.round(rx * $("#bPic").height()) + "px",	//预览图片高度为计算比例值与原图片高度的乘积
                    marginLeft:"-" + Math.round(rx * coords.x) + "px",
                    marginTop:"-" + Math.round(ry * coords.y) + "px"
                });
            }
        }
    },

    ajaxGET:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"GET", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    },
    ajaxPOST:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"POST", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    }

}
$(function(){
    enterpriseLogo.init();
})
//弹出层居中插件
;(function($){
    $.fn.extend({
        setPosition:function(){
            if(this.height() < $(window).height()) {
                this.css({"top":($(window).height() - this.height())/2 + $(document).scrollTop()});
            }else{
                this.css({top:$(document).scrollTop()});
            }
            this.css({"left":($(window).width() - this.width())/2});
            return this;
        }
    });
})(jQuery);